package osslog

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
	"github.com/xuelang-group/suanpan-appconnector/pkg/util"
)

func tearDown(tempPath string) {
	os.Remove(tempPath)
}

func TestOssLocalFileCreate(t *testing.T) {
	Convey("File create", t, func() {
		defer tearDown("/tmp/testoss1")
		_, err := NewOssLogHelper(OSS_LOG_HELPER_DEFAULT_INTERVAL_SECOND, 1, 1, "null", "/tmp/testoss1", false, false)
		So(err, ShouldBeNil)
		log.Println("")
		e, err := util.FileExists("/tmp/testoss1")
		fmt.Println(err)
		So(err, ShouldBeNil)
		So(e, ShouldEqual, true)
	})
}

func TestOssLocalFileRotate(t *testing.T) {
	Convey("file rotate test", t, func() {
		path := "/tmp/testossrotate"
		defer tearDown(path)
		h, err := NewOssLogHelper(OSS_LOG_HELPER_DEFAULT_INTERVAL_SECOND, 1, 1, "null", path, false, true)
		So(err, ShouldBeNil)

		arr := make([]string, 0)
		h.PushFunc = func(localPath, remotePath string) {
			_ = remotePath
			arr = append(arr, localPath)
		}
		// 写入1M内容
		const size = 1024
		for i := 0; i < size; i++ {
			var buffer bytes.Buffer
			for i := 0; i < size; i++ {
				buffer.WriteByte('a') // 你可以选择其他字符
			}
			log.Println(buffer.String())
		}

		var files []string
		err = filepath.Walk("/tmp", func(path string, info os.FileInfo, err error) error {
			_ = err
			//remove the . file
			if info != nil && !info.IsDir() && strings.Contains(path, "testossrotate-") == true {
				files = append(files, path)
			}
			return nil
		})
		So(err, ShouldBeNil)
		for _, f := range files {
			tearDown(f)
		}

		So(len(files), ShouldEqual, 1)
		log.Println("1m") // < 1M not rotate

		readstr := h.ReadCurrentLogToList()
		So(readstr, ShouldNotBeNil)
		So(readstr[1], ShouldEqual, "1m")
	})
}

func TestOssTimePush(t *testing.T) {
	Convey("Test oss time push", t, func() {

		path := "/tmp/testosspushtime"
		defer tearDown(path)
		h, err := NewOssLogHelper(1, 1, 1, "null", path, false, true)
		So(err, ShouldBeNil)
		arr := make([]string, 0)
		h.PushFunc = func(localPath, remotePath string) {
			_ = remotePath
			arr = append(arr, localPath)
		}

		log.Println("123")
		log.Println("456")
		So(arr, ShouldBeEmpty)
		time.Sleep(1 * time.Second)
		log.Println("7")
		//wait goroutine finished
		time.Sleep(50 * time.Microsecond)

		So(arr[0], ShouldEqual, path)

		s := h.ReadCurrentLogToList()
		So(s, ShouldNotBeNil)
		So(s[0], ShouldEqual, "123")
		So(s[1], ShouldEqual, "456")
		So(s[2], ShouldEqual, "7")
	})

}

func TestOssTimeAutoPush(t *testing.T) {
	Convey("test file auto push", t, func() {

		path := "/tmp/testosspushtimeauto"
		defer tearDown(path)
		h, err := NewOssLogHelper(1, 1, 1, "null", path, true, true)
		So(err, ShouldBeNil)
		arr := make([]string, 0)
		h.PushFunc = func(localPath, remotePath string) {
			_ = remotePath
			arr = append(arr, localPath)
		}

		log.Println("123")
		log.Println("456")
		So(arr, ShouldBeEmpty)
		time.Sleep(1 * time.Second)
		time.Sleep(50 * time.Microsecond)
		//push complete auto
		So(arr[0], ShouldEqual, path)

		s := h.ReadCurrentLogToList()
		So(s, ShouldNotBeNil)
		So(s[0], ShouldEqual, "123")
		So(s[1], ShouldEqual, "456")
	})
}

func TestOssNotPushToOss(t *testing.T) {
	Convey("Test oss time push", t, func() {

		path := "/tmp/testosspushtime"
		defer tearDown(path)
		h, err := NewOssLogHelper(1, 5000, 0, "null", path, false, false)
		So(err, ShouldBeNil)
		arr := make([]string, 0)
		h.PushFunc = func(localPath, remotePath string) {
			arr = append(arr, localPath)
		}

		log.Println("123")
		log.Println("456")
		So(arr, ShouldBeEmpty)
		time.Sleep(1 * time.Second)
		log.Println("7")
		//wait goroutine finished
		time.Sleep(50 * time.Microsecond)

		So(len(arr), ShouldEqual, 0)
	})
}
